package com.itheima.util;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.HashMap;
import java.util.Map;

//JWT的生成和解析
public class JwtUtil {
    // 创建token
    public static String createToken(Map claims) {
        return Jwts.builder()
                .setClaims(claims) //设置响应数据体
                .signWith(SignatureAlgorithm.HS256, ConstantUtil.JWT_SECRET) //设置加密方法和加密盐
                .compact();
    }

    // 解析token
    public static Map parseToken(String token) {
        return Jwts.parser().setSigningKey(ConstantUtil.JWT_SECRET)
                .parseClaimsJws(token)
                .getBody();
    }


    public static void main(String[] args) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("id",123);
        map.put("name","tom");
        map.put("pwd","asdqwe123");

        String token = createToken(map);
        System.out.println(token);


        Map map1 = parseToken("eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoidG9tIiwiaWQiOjEyMywicHdkIjoiYXNkcXdlMTIzIn0.7RFqiBWPoeezLTY9415UcoNLlE5nt3V6HT7OjMduTo0");
        System.out.println(map1);
    }
}
